package com.mego.tzadd.activitys;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.mego.tzadd.R;
import com.mego.tzadd.database.manager.SystemCenter;
import com.mego.tzadd.utils.HttpRequest;

import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.interfaces.OnHttpResponseListener;
import zuo.biao.library.util.JSON;

//问题反馈
public class ProfileActivity extends BaseActivity implements View.OnClickListener {

    private final String TAG = "ProfileActivity";

    //启动方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    public static Intent createIntent(Context context) {
        return new Intent(context, com.mego.tzadd.activitys.ProfileActivity.class);
    }

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.profile_activity);

        initView();
        initData();
        initEvent();

    }

    @Override
    public void initData() {


    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }


    @Override
    public void onForwardClick(View v) {

    }

    private EditText edit_username;
    private EditText edit_email;
    private EditText edit_mobile;
    private TextView tv_version;

    @Override
    public void initView() {
        ImageButton ivBaseTabReturn = (ImageButton) findViewById(R.id.ivBaseTabReturn);
        if (ivBaseTabReturn != null) {
            ivBaseTabReturn.setScaleType(ImageView.ScaleType.FIT_START);
        }
        edit_username = (EditText) findViewById(R.id.edit_username);
        edit_email = (EditText) findViewById(R.id.edit_email);
        edit_mobile = (EditText) findViewById(R.id.edit_mobile);

        edit_username.setText(SystemCenter.getInstance().username);
        edit_email.setText(SystemCenter.getInstance().email);
        edit_mobile.setText(SystemCenter.getInstance().email);

        tv_version = (TextView) findViewById(R.id.tv_version);

        String curVersionName = "";
        try {
            PackageInfo info = getPackageManager().getPackageInfo(getPackageName(), 0);
            curVersionName = info.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace(System.err);
        }

        tv_version.setText("版本號：" + curVersionName);
    }

    @Override
    public void initEvent() {
        findViewById(R.id.tv_xgmm).setOnClickListener(this);
        findViewById(R.id.tv_queren).setOnClickListener(this);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.tv_queren:
                onQueren();
                break;
            case R.id.tv_xgmm:
                onXgmm();
                break;
        }
    }

    private void onXgmm() {
        Intent intent = new Intent(this, WjmmActivity.class);
        startActivityForResult(intent, 13);
    }

    private void onQueren() {
        final String email = edit_mobile.getText().toString();
        showProgressDialog(R.string.changing);
        try {
            HttpRequest.changuserinfo(SystemCenter.getInstance().adminid, email, 0, new OnHttpResponseListener() {
                @Override
                public void onHttpResponse(int requestCode, String resultJson, Exception e) {
                    JSONObject jsonObject = JSON.parseObject(resultJson);
                    int code = 0;
                    if (jsonObject != null) {
                        code = jsonObject.getInteger("code");
                    }
                    if (code == 1) {
                        showShortToast("修改成功！");
                        SystemCenter.getInstance().email = email;
                    } else {
                        showShortToast("修改失败！");
                    }
                    dismissProgressDialog();
                    finish();
                }
            });
        } catch (Exception e1) {
            showShortToast("網絡無連接！");
            dismissProgressDialog();
        }

    }
}


